package com.woniu.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.product.pojo.ServiceType;
import com.woniu.product.mapper.ServiceTypeMapper;
import com.woniu.product.pojo.ServiceType;
import com.woniu.product.service.IServiceTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * 服务类型表 服务实现类
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-15
 */
@Service
public class ServiceTypeServiceImpl extends ServiceImpl<ServiceTypeMapper, ServiceType> implements IServiceTypeService {

    @Autowired(required = false)
    private ServiceTypeMapper serviceTypeMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void addServiceType(ServiceType serviceType) {
        int insert = serviceTypeMapper.insert(serviceType);

        CorrelationData correlationData = new CorrelationData();
        Message mess = new Message(serviceType.getServiceType().getBytes(StandardCharsets.UTF_8));
        correlationData.setReturnedMessage(mess);
        rabbitTemplate.convertAndSend("service_type_exchange","add_service_type","新增服务类别：" + serviceType.getServiceType() + "  ， 请前往门店端进行查看！",correlationData);

    }

    @Override
    public void deleteServiceType(String id) {
        int deleteById = serviceTypeMapper.deleteById(id);

    }

    @Override
    public void updateServiceType(ServiceType serviceType) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("service_type",serviceType.getServiceType());
        updateWrapper.set("description",serviceType.getDescription());
        updateWrapper.eq("id",serviceType.getId());
        baseMapper.update(serviceType,updateWrapper);
        // int updateById = serviceTypeMapper.updateById(serviceType);
    }

    @Override
    public PageResult<ServiceType> queryServiceType(Integer currentPage, Integer pageSize, String serviceTypeName) {
        IPage page = new Page(currentPage , pageSize);
        LambdaQueryWrapper<ServiceType> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ServiceType::getServiceType,serviceTypeName);
        IPage selectPage = serviceTypeMapper.selectPage(page, wrapper);
        return new PageResult<ServiceType>(selectPage.getTotal(),selectPage.getRecords());
    }
}
